namespace System.Linq.Reactive
{
    class TakeObserver<T> : SimpleObserverEx<T>
    {
        private readonly int f_Count;
        private int f_Position;

        public override void OnNext(T item)
        {
            if(f_Position >= f_Count) return;
            base.OnNext(item);
            if(f_Position == f_Count - 1) OnComplited();
            f_Position++;
        }

        public override void OnComplited()
        {
            if(f_Position >= f_Count) return;
            base.OnComplited();
        }

        public override void OnException(Exception error)
        {
            if(f_Position >= f_Count) return;
            base.OnException(error);
        }

        public override void OnReset()
        {
            base.OnReset();
            f_Position = 0;
        }

        public TakeObserver(IObserverEx<T> observer, int Count)
        {
            observer.Next += OnNext;
            observer.Reset += OnReset;
            observer.Complited += OnComplited;
            observer.Exception += OnException;
            f_Count = Count;
        }
    }
}